<!-- 专区管理 -->
<template>
  <div class="zoneManage sys-content">
    <page-mod>
      <el-card>
        <div class="query-box">
          <el-form :model="query" inline>
            <el-form-item label="专区名称:">
              <el-input v-model="query.zoneName" placeholder="请输入商品名称" clearable/>
            </el-form-item>
            <el-form-item label="状态:">
              <el-select v-model="query.enableStatus" placeholder="请选择" clearable>
                <el-option label="启用" value="1" />
                <el-option label="禁用" value="0" />
              </el-select>
            </el-form-item>
          </el-form>
          <div class="btns">
            <el-button icon="Search" color="#16b777" @click="load(1)">查询</el-button>
            <el-button icon="Plus" color="#FFB800" @click="show = true">新增</el-button>
            <el-button icon="Refresh" color="#FF5722" @click="resetSearch">重置</el-button>
          </div>
        </div>
      </el-card>
    </page-mod>
    <page-mod title="专区数据" icon="Histogram" :showBtn="false" :padBottom="false">
      <el-card v-loading="loading" element-loading-text="拼命加载中" >
        <el-table :data="dataList" max-height="493" border>
          <el-table-column label="专区名称" prop="zoneName" align="center"></el-table-column>
          <el-table-column label="专区图标" prop="zoneIcon" align="center">
            <template #default="{row}">
              <div class="img-cell">
                <el-image :src="row.zoneIcon + '?x-oss-process=image/resize,m_fill,h_40,w_40'"/>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="专区地址" prop="zoneUrl" align="center"></el-table-column>
          <el-table-column label="启用状态" prop="enableStatus" align="center">
            <template #default="{row}">
              <el-tag :type="row.enableStatus ? 'success' : 'danger'">{{ row.enableStatus ? '已启用' : '已禁用' }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="orderNum" align="center"></el-table-column>
          <el-table-column label="操作" align="center" width="350">
            <template #default="{row}">
              <el-button type="primary" icon="Edit" link @click="toUpdate(row.productZoneId)">修改</el-button>
              <el-divider direction="vertical" />
              <el-button type="danger" icon="Delete" link @click="doDelete(row.productZoneId)">删除</el-button>
              <el-divider direction="vertical" />
              <el-button
                :type="row.enableStatus ? 'danger' : 'success'" 
                :icon="row.enableStatus ? 'WarnTriangleFilled' : 'SuccessFilled'"
                link 
                @click="doPublish(row)">
                {{ row.enableStatus ? '禁用' : '启用' }}
              </el-button>
              <el-divider direction="vertical" />
              <el-button type="primary" icon="Goods" link @click="toUpdate(row.productZoneId)">添加商品</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination 
          class="paging"
          v-show="query.total"
          small
          layout="total, sizes, prev, pager, next, jumper" 
          :current-page="query.current" 
          :page-size="query.size"
          :total="query.total"
          @current-change="load"
          @size-change="sizeChange">
        </el-pagination>
      </el-card>
    </page-mod>

    <!-- 新增、编辑弹窗 -->
    <el-dialog v-model="show" :title="form.productZoneId ? '编辑' : '新增'" top="10vh" width="23%" center draggable @close="resetForm">
      <el-form ref="formRef" :model="form" :rules="rules" size="default" label-width="95px">
        <el-form-item label="专区名称:" prop="zoneName" required>
          <el-input v-model="form.zoneName" placeholder="请输入专区名称" clearable/>
        </el-form-item>
        <el-form-item label="专区图标:" prop="zoneIcon" required>
          <upload-img v-model="form.zoneIcon" width="60" height="60" iconSize="20" iswatch></upload-img>
        </el-form-item>
        <el-form-item label="专区地址:" prop="zoneUrl" required>
          <el-input v-model="form.zoneUrl" placeholder="请输入专区地址" clearable/>
        </el-form-item>
        <el-form-item label="排序:" prop="orderNum">
          <el-input-number v-model="form.orderNum" :min="1"></el-input-number>
        </el-form-item>
        <el-form-item label="是否启用:" prop="enableStatus">
          <el-switch 
            v-model="form.enableStatus"
            :active-value="1"
            :inactive-value="0"
            style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" size="default" @click="doSubmit">保存</el-button>
        <el-button size="default" @click="show = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="zoneManage">
  import { cudZoneApi, getZoneListPagesApi, getZoneInfoApi, updateZoneStatusApi } from '@/api/api'
  import { ref, reactive, toRefs, getCurrentInstance, onActivated } from 'vue'

  // 获取注册的全局实列
  const currentThis = getCurrentInstance().appContext.config.globalProperties

  const formRef = ref(null)
  
  const currentData = reactive({
    loading: false,
    show: false,
    query: {
      zoneName: '',
      enableStatus: '',
      current: 1,
      size: 20,
      total: 0
    },
    dataList: [],
    form: {
      productZoneId: '',
      zoneName: '',
      zoneIcon: '',
      zoneUrl: '',
      orderNum: 1,
      enableStatus: 1
    },
    rules: {
      zoneName: [{ required: true, message: '请输入专区名称', trigger: ['blur', 'change'] }],
      zoneIcon: [{ required: true, message: '请上传专区图标', trigger: ['blur', 'change'] }],
      zoneUrl: [{ required: true, message: '请输入专区地址', trigger: ['blur', 'change'] }]
    }
  })
  let {loading, show, query, dataList, form, rules} = toRefs(currentData)

  // 查询
  function load(page) {
    currentData.loading = true
    currentData.query.current = page
    getZoneListPagesApi(currentData.query).then(res => {
      currentData.loading = false
      if(res.code === '200') {
        currentData.dataList = res.data.pages
        currentData.query.total = res.data.total
      } else {
        currentThis.$message.error(res.message)
      }
    })
  }

  // 新增或编辑
  function doSubmit() {
    formRef.value.validate((valid) => {
      if (valid) {
        currentThis.$messageBox.confirm(`确认${currentData.form.productZoneId ? '修改' : '新增'}吗？`).then(() => {
          let loading = currentThis.$loading(`正在${currentData.form.productZoneId ? '修改' : '新增'}`)
          cudZoneApi(`${currentData.form.productZoneId ? 'update' : 'save'}`, currentData.form).then(res => { // 修改
            loading.close()
            if(res.code === '200') {
              load(currentData.form.productZoneId ? currentData.query.current : 1)
              currentData.show = false
              currentThis.$message.success(res.message)
            } else {
              currentThis.$message.error(res.message)
            }
          })
        }).catch(() => {})
      } else {
        return false;
      }
    })
  }

  // 修改前弹窗
  function toUpdate(productZoneId) {
    getZoneInfoApi(productZoneId).then(res => {
      if(res.code === '200') {
        currentData.form = res.data
        currentData.show = true
      } else {
        currentThis.$message.error(res.message)
      }
    })
  }

  // 删除
  function doDelete(productZoneId) {
    currentThis.$messageBox.confirm('确认删除吗？').then(() => {
      let loading = currentThis.$loading('删除中，请耐心等待')
      cudZoneApi('delete', { id: productZoneId }).then(res => {
        loading.close()
        if(res.code === '200') {
          currentThis.$message.success(res.message)
          load(currentData.query.current)
        } else {
          currentThis.$message.error(res.message)
        }
      })
    }).catch(() => {})
  }

  // 启用或禁用
  function doPublish(e) {
    currentThis.$messageBox.confirm(`确认${e.enableStatus ? '禁用' : '启用'}吗？`).then(() => {
      const loading = currentThis.$loading('正在处理，请耐心等待')
      updateZoneStatusApi(e.productZoneId, e.enableStatus ? 0 : 1).then(res => {
        loading.close()
        if(res.code === '200') {
          load(currentData.query.current)
          currentThis.$message.success(res.message)
        } else {
          currentThis.$message.error(res.message)
        }
      })
    }).catch(() => {})
  }

  // 新增弹窗重置数据
  function resetForm() {
    currentData.form = {
      productZoneId: '',
      zoneName: '',
      zoneIcon: '',
      zoneUrl: '',
      orderNum: 1,
      enableStatus: 1
    }
    formRef.value.resetFields()
  }

  // 重置查询
  function resetSearch() {
    currentData.query.zoneName = ''
    currentData.query.enableStatus = ''
    load(1)
  }

  function sizeChange(size) {
    currentData.query.size = size
    load(1)
  }

  onActivated(() => {
    load(currentData.query.current)
  })
</script>

<style lang="less" scoped>
.zoneManage{
  :deep(.el-table) {
    .img-cell{
      display: flex;
      justify-content: center;
      align-items: center;
      .el-image{
        width: 40px; 
        height: 40px;
        border-radius: 5px;
      }
    }
  }
  :deep(.el-dialog){
    .el-input{
      width: 300px;
    }
    .zoneIcon_upload{
      width: 60px; 
      height: 60px;
      border-radius: 8px;
      border: 1px dashed #DCDFE6;
      cursor: pointer;
      display: flex;
      justify-content: center;
      align-items: center;
      &:hover{
        border-color: #409EFF;
      }
    }
  }
}
</style>